﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_AssetWarranty
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_AssetWarranty> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_AssetWarranty> infoList = new List<M_AssetWarranty>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT warranty_seq,asset_code,warranty_desc,warranty_remark "
                        +" ,warranty_start_date,warranty_end_date "
                        +" FROM icm_asset_warranty and record_status<>'D'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_AssetWarranty info = new M_AssetWarranty();
                        info.warranty_seq = Convert.ToInt32(drc[i]["warranty_seq"]);
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.warranty_desc = drc[i]["warranty_desc"].ToString();
                        info.warranty_remark = drc[i]["warranty_remark"].ToString();
                        info.warranty_start_date = Convert.ToDateTime(drc[i]["warranty_start_date"]);
                        info.warranty_end_date = cn.ConvertNullDateTime(drc[i]["warranty_end_date"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_AssetWarranty GetListFillBySeq(string warranty_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_AssetWarranty info = new M_AssetWarranty();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@warranty_seq", warranty_seq);

                strSQL = " SELECT warranty_seq,asset_code,warranty_desc,warranty_remark "
                        + " ,warranty_start_date,warranty_end_date "
                        + " FROM icm_asset_warranty "
                        + " WHERE  warranty_seq = @warranty_seq and record_status<>'D' "
                        + " ORDER BY warranty_start_date ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.warranty_seq = Convert.ToInt32(drc[0]["warranty_seq"]);
                    info.asset_code = drc[0]["asset_code"].ToString();
                    info.warranty_desc = drc[0]["warranty_desc"].ToString();
                    info.warranty_remark = drc[0]["warranty_remark"].ToString();
                    info.warranty_start_date = Convert.ToDateTime(drc[0]["warranty_start_date"]);
                    info.warranty_end_date = cn.ConvertNullDateTime(drc[0]["warranty_end_date"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public IList<M_AssetWarranty> GetListAssetCode(string asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_AssetWarranty> infoList = new List<M_AssetWarranty>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT warranty_seq,asset_code,warranty_desc,warranty_remark "
                        + " ,warranty_start_date,warranty_end_date "
                        + " FROM icm_asset_warranty "
                        + " WHERE record_status<>'D' and asset_code LIKE '%' '" + asset_code + "'  '%' "
                        + " ORDER BY warranty_start_date DESC ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_AssetWarranty info = new M_AssetWarranty();
                        info.warranty_seq = Convert.ToInt32(drc[i]["warranty_seq"]);
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.warranty_desc = drc[i]["warranty_desc"].ToString();
                        info.warranty_remark = drc[i]["warranty_remark"].ToString();
                        info.warranty_start_date = Convert.ToDateTime(drc[i]["warranty_start_date"]);
                        info.warranty_end_date = cn.ConvertNullDateTime(drc[i]["warranty_end_date"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
       
        public void Insert_Asset_Warranty(M_AssetWarranty info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);
                dbManager.AddParameters(0, "@asset_code", info.asset_code);
                dbManager.AddParameters(1, "@warranty_desc", info.warranty_desc);
                dbManager.AddParameters(2, "@warranty_remark", info.warranty_remark);
                dbManager.AddParameters(3, "@warranty_start_date", info.warranty_start_date);
                dbManager.AddParameters(4, "@warranty_end_date", info.warranty_end_date);
                dbManager.AddParameters(5, "@record_status", info.record_status);
                dbManager.AddParameters(6, "@created_by", info.created_by);
                dbManager.AddParameters(7, "@created_when", info.created_when);

                strSQL = " INSERT INTO icm_asset_warranty "
                        + " (asset_code,warranty_desc,warranty_remark,warranty_start_date,warranty_end_date,record_status,created_by,created_when ) "
                        + " VALUES "
                        + " (@asset_code,@warranty_desc,@warranty_remark,@warranty_start_date,@warranty_end_date,@record_status,@created_by,@created_when ) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_Asset_Warranty(M_AssetWarranty info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);
                dbManager.AddParameters(0, "@warranty_seq", info.warranty_seq);
                dbManager.AddParameters(1, "@asset_code", info.asset_code);
                dbManager.AddParameters(2, "@warranty_desc", info.warranty_desc);
                dbManager.AddParameters(3, "@warranty_remark", info.warranty_remark);
                dbManager.AddParameters(4, "@warranty_start_date", info.warranty_start_date);
                dbManager.AddParameters(5, "@warranty_end_date", info.warranty_end_date);
                dbManager.AddParameters(6, "@record_status", info.record_status);
                dbManager.AddParameters(7, "@updated_by", info.updated_by);
                dbManager.AddParameters(8, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_asset_warranty "
                        + " SET asset_code = @asset_code "
                        + " ,warranty_desc = @warranty_desc "
                        + " ,warranty_remark = @warranty_remark "
                        + " ,warranty_start_date =@warranty_start_date "
                        + " ,warranty_end_date = @warranty_end_date "
                        + " ,record_status = @record_status "
                        + " ,updated_by = @updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE warranty_seq = @warranty_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete_Asset_Warranty(string asset_code_start, string asset_code_finish,string remark)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@asset_code_start", asset_code_start);
                dbManager.AddParameters(1, "@asset_code_finish", asset_code_finish);
                dbManager.AddParameters(2, "@remark" ,remark);

                strSQL = " UPDATE icm_asset_warranty "
                        + " SET record_status = 'D' "
                        + " ,remark = @remark "
                        + " WHERE asset_code >=  '@asset_code_start' AND "
                        + " asset_code <= '@asset_code_finish' ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete_Warranty(M_AssetWarranty info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@warranty_seq", info.warranty_seq);
                dbManager.AddParameters(1, "@record_status", info.record_status);
                dbManager.AddParameters(2, "@warranty_end_date", info.warranty_end_date);

                strSQL = " UPDATE icm_asset_warranty "
                        + " SET record_status = @record_status "
                        + " ,warranty_end_date = @warranty_end_date "
                        + " WHERE warranty_seq =  @warranty_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}

